package com.partner.springsecurity;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.*;
import org.springframework.security.access.intercept.*;
import org.springframework.security.web.*;
import org.springframework.security.web.access.intercept.*;

public class SysSecurityFilter extends AbstractSecurityInterceptor implements Filter
{
   private FilterInvocationSecurityMetadataSource securityMetadataSource;

   public FilterInvocationSecurityMetadataSource getSecurityMetadataSource()
   {
      return securityMetadataSource;
   }

   public void setSecurityMetadataSource(FilterInvocationSecurityMetadataSource securityMetadataSource)
   {
      this.securityMetadataSource = securityMetadataSource;
   }

   
   public void destroy()
   {
      // TODO Auto-generated method stub
      
   }

   
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
   {
      // TODO Auto-generated method stub
      FilterInvocation filterInvocation = null;
      InterceptorStatusToken statusToken = null;
      
      HttpServletRequest httpRequest = (HttpServletRequest)request;
      String requestUri = httpRequest.getRequestURI();
      String requestUrl = httpRequest.getRequestURL().toString();
      System.out.println("SysSecurityFilter::doFilter - URI[" + requestUri + "] URL[" + requestUrl + "]");
      
      filterInvocation = new FilterInvocation(request, response, filterChain);
      statusToken = super.beforeInvocation(filterInvocation);
      try
      {
         filterInvocation.getChain().doFilter(filterInvocation.getRequest(), filterInvocation.getResponse());
      }
      catch(Exception eFilter)
      {
         eFilter.printStackTrace();
      }
      finally
      {
         super.afterInvocation(statusToken, null);
      }
   }

   
   public void init(FilterConfig arg0) throws ServletException
   {
      // TODO Auto-generated method stub
      
   }

   @Override
   public Class<?> getSecureObjectClass()
   {
      // TODO Auto-generated method stub
      return FilterInvocation.class;
   }

   @Override
   public SecurityMetadataSource obtainSecurityMetadataSource()
   {
      // TODO Auto-generated method stub
      return this.securityMetadataSource;
   }
}
